import request from '@/utils/request'

const BASE_URL = '/api/yyplatform/admin/sys/menu'

class MenuAPI {
  static getList() {
    return request<any, MenuDTO[]>({
      url: `${BASE_URL}/query`,
      method: 'get'
    })
  }
  static getFormData(id?: number): Promise<MenuFormDTO | undefined> {
    return request<any, MenuFormDTO>({
      url: `${BASE_URL}/get?id=${id}`,
      method: 'get'
    })
  }

  static getOptions(onlyRoot?: boolean) {
    return request<any, MenuDTO[]>({
      url: `${BASE_URL}/options`,
      method: 'get',
      params: {
        onlyRoot
      }
    })
  }

  /**
   * 添加菜单
   *
   * @param data 菜单表单数据
   * @returns 请求结果
   */
  static add(data: MenuFormDTO) {
    return request({
      url: `${BASE_URL}/save`,
      method: 'post',
      data: data
    })
  }

  /**
   * 修改菜单
   *
   * @param id 菜单ID
   * @param data 菜单表单数据
   * @returns 请求结果
   */
  static update(id: string, data: MenuFormDTO) {
    return request({
      url: `${BASE_URL}/update`,
      method: 'post',
      data: data
    })
  }

  static deleteById(id: number) {
    return request({
      url: `${BASE_URL}/delete`,
      method: 'post',
      data: {
        id: id
      },
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

export default MenuAPI

export interface MenuDTO {
  id?: number
  pid?: number
  menuType: string
  name: string
  label: string
  value: number
  permissions: string
  orderNum: number
  visible: boolean
  remark: string
  component: string
  enabled: boolean
  icon: string
  children: MenuDTO[]
  hasChildren?: boolean
  routePath: string
  routeName: string
  keepAlive: boolean
  alwaysShow: boolean
  params?: KeyValue[]
}

export interface MenuFormDTO {
  id?: number
  pid?: number
  routePath: string
  routeName: string
  menuType: string
  name: string
  permissions: string
  orderNum: number
  visible: boolean
  remark: string
  component: string
  enabled: boolean
  icon: string
  keepAlive: boolean
  alwaysShow: boolean
  params?: KeyValue[]
}

export interface KeyValue {
  key: string
  value: any
}
